package com.ruifu.pls.service;


import com.ruifu.pls.entity.DocAsnDetails;
import com.ruifu.pls.entity.DocAsnHeader;
import com.ruifu.pls.model.GLCallInputParam;
import com.ruifu.pls.model.ansReverseReturnAdd.GLAsnReverseReturnAdd;
import com.ruifu.pls.model.asnAddConfirm.GAsnHeader;
import com.ruifu.pls.model.asnReturnConform.GLAsnReturnConform;
import com.ruifu.pls.model.asnReverseAdd.GLDataDetail;
import com.ruifu.pls.repository.GAsndetailRepository;
import com.ruifu.pls.repository.GAsnhederRepository;
import com.ruifu.pls.repository.SkuRepository;
import com.ruifu.pls.utils.HttpUtils;
import com.ruifu.pls.utils.SighUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.List;

@Service
public class AsnheaderService {

    @Autowired
    GAsnhederRepository gAsnhederRepository ;

    @Autowired
    GAsndetailRepository gAsndetailRepository;

    @Autowired
    SkuRepository skuLogRepository;

    private org.slf4j.Logger logger = LoggerFactory.getLogger(DocAsnHeader.class);

    //正向入库确认
    @Transactional
    public void asnConform() {
        DocAsnHeader docAsnHeader = gAsnhederRepository.findAsnForAddConform();

        if (docAsnHeader != null) {
            String asnno = docAsnHeader.getAsnno();
            String status = docAsnHeader.getAsnstatus();
            List<DocAsnDetails> docAsnDetails = gAsndetailRepository.findAsnDetails(asnno);
            //际链入库头
            GAsnHeader gAsnHeader = docAsnHeader.gAsnHeader;
            //际链入库明细
            //把查询到的wms明细加入到际链model明细
            for (int i = 0; i < docAsnDetails.size(); i++) {
                gAsnHeader.orderLines.add(docAsnDetails.get(i).gAsnDetail);
            }
            GAsnHeader glReturnAsnInputRaram = gAsnHeader;
            GLCallInputParam glCallInputParam = new GLCallInputParam();
            glCallInputParam.setData(glReturnAsnInputRaram);//设置请求数据
            glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
            glCallInputParam.setMethod("g2matrix.entryorder.unloadingOK");

                 logger.info(glCallInputParam.toString());
                    try {
                        String responsse = HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());
                        //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {
                            gAsnhederRepository.updateAsnStatus(asnno);
                        } else {
                            gAsnhederRepository.updateAsnStatusEorro(asnno);
                            logger.info(responsse);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }


    //正向退货入库确认
    @Transactional
    public void asnReturnConform() {
        DocAsnHeader docAsnHeader = gAsnhederRepository.findAsnForReturnConform();


        if(docAsnHeader != null)
        {
            String asnno  = docAsnHeader.getAsnno();
            String status = docAsnHeader.getAsnstatus();
            List<DocAsnDetails> docAsnDetails = gAsndetailRepository .findAsnDetails(asnno);
            GLAsnReturnConform gLAsnHeader = docAsnHeader.glAsnReturnConform;
            for(int i=0; i<docAsnDetails.size();i++)
            {
                gLAsnHeader.orderLines.add(docAsnDetails.get(i).glAsnReturnConformDetail);
            }

            GLCallInputParam glCallInputParam = new GLCallInputParam();
            glCallInputParam.setData(gLAsnHeader);//设置请求数据
            glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
            glCallInputParam.setMethod("g2matrix.entryorder.returnUnloadingOK");

                    logger.info(glCallInputParam.toString());
                    try {
                        String responsse  = HttpUtils.doPost("http://demo.api.g2link.cn:8081/","router",glCallInputParam.toString());
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {
                            gAsnhederRepository.updateAsnStatus(asnno);
                        } else {
                            gAsnhederRepository.updateAsnStatusEorro(asnno);
                            logger.info(responsse);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }


    //入库逆向新增
    @Transactional
    public void asnReverseAdd() {

        DocAsnHeader docAsnHeader = gAsnhederRepository.findAsnForReverseAdd();



        if (docAsnHeader != null) {
            String asnno  = docAsnHeader.getAsnno();
            List<DocAsnDetails> docAsnDetails = gAsndetailRepository.findAsnDetails(docAsnHeader.getAsnno());
            GLDataDetail gAsnHeader = docAsnHeader.glDataDetail;
            for (int i = 0; i < docAsnDetails.size(); i++) {
                gAsnHeader.orderLines.add(docAsnDetails.get(i).glOrderLines);
            }
            GLDataDetail glReverseAsnInputRaram = gAsnHeader;
            GLCallInputParam glCallInputParam = new GLCallInputParam();
            glCallInputParam.setData(glReverseAsnInputRaram);//设置请求数据
            glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
            glCallInputParam.setMethod("g2matrix.entryorder.creatUnloadingInfor");
            logger.info(glCallInputParam.toString());
            try {
                String responsse  = HttpUtils.doPost("http://demo.api.g2link.cn:8081/","router",glCallInputParam.toString());
                if (responsse.contains("\"code\":0")) {
                    gAsnhederRepository.updateAsnStatusBy(asnno);
                    logger.info("退入库逆向新增"+responsse);
                } else {
                    gAsnhederRepository.updateAsnStatusByEE(asnno);
                    logger.info(responsse);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    //退入库逆向新增
    @Transactional
    public void asnReverseReturnAadd() {
        DocAsnHeader docAsnHeader = gAsnhederRepository.findAsnForReturnConformAdd();




        if(docAsnHeader != null)

        {
            GLAsnReverseReturnAdd glAsnReverseReturnAdd = docAsnHeader.glAsnReverseReturnAdd;
            String asnno  = docAsnHeader.getAsnno();
            List<DocAsnDetails> details = gAsndetailRepository.findAsnDetails(docAsnHeader.getAsnno());

            for(int i=0;i<details.size();i++){
                glAsnReverseReturnAdd.orderLines.add(details.get(i).glAsnReverseReturnAddDetail);
            }

            GLCallInputParam glCallInputParam = new GLCallInputParam();
            glCallInputParam.setData(glAsnReverseReturnAdd);//设置请求数据
            glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
            glCallInputParam.setMethod("g2matrix.entryorder.creatreturnUnloadingInfor");

            logger.info("退货入库新增接口"+glCallInputParam.toString());
            try {
                String responsse  = HttpUtils.doPost("http://demo.api.g2link.cn:8081/","router",glCallInputParam.toString());
                if (responsse.contains("\"code\":0")) {
                    gAsnhederRepository.updateAsnStatusBy(asnno);
                } else {
                    gAsnhederRepository.updateAsnStatusByEE(asnno);
                    logger.info("退货入库新增接口"+responsse);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


}